
#include <gtk/gtk.h>
#include <libglade-2.0/glade/glade.h>
#include <locale>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../utility/assert_helper.h"
#include "../hive_access/nt_hive.h"
#include "../windows_manager/windows_info.h"
#include "../windows_manager/sys_search.h"
#include "../windows_manager/reg_accessor.h"

void
some_handler(GtkWidget *widget)
{
    /* a handler referenced by the glade file.  Must not be static
     * so that it appears in the global symbol table. */
}

#include <gtk/gtk.h>

enum
{
  COL_NAME = 0,
  COL_TYPE,
  COL_DATA,
  NUM_COLS
};

enum
{
  COLUMN = 0,
  COLS_NUM
};

GtkListStore  *store;
GtkTreeStore *treestore;

GtkTreeModel *
create_and_fill_value_model (void)
{

  GtkTreeIter    iter;

  store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT);

  /* Append a row and fill in some data */
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter,
                      COL_NAME, "Heinz El-Mann",
                      COL_TYPE, 51,
                      COL_DATA, 0,
                      -1);

  /* append another row and fill in some data */
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter,
                      COL_NAME, "Jane Doe",
                      COL_TYPE, 23,
                      COL_DATA, 0,
                      -1);

  /* ... and a third row */
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter,
                      COL_NAME, "Joe Bungop",
                      COL_TYPE, 91,
                      COL_DATA, 0,
                      -1);

  return GTK_TREE_MODEL (store);
}

GtkWidget *
create_value_view_and_model (GtkWidget *view)
{
  GtkCellRenderer     *renderer;
  GtkTreeModel        *model;

  //view = gtk_tree_view_new ();

  /* --- Column #1 --- */

  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1,
                                               "Name",
                                               renderer,
                                               "text", COL_NAME,
                                               NULL);

  /* --- Column #2 --- */

  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1,
                                               "Type",
                                               renderer,
                                               "text", COL_TYPE,
                                               NULL);
  /* --- Column #3 --- */

  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1,
                                               "Data",
                                               renderer,
                                               "text", COL_DATA,
                                               NULL);

  model = create_and_fill_value_model ();

  gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);

  /* The tree view has acquired its own reference to the
   *  model, so we can drop ours. That way the model will
   *  be freed automatically when the tree view is destroyed */

  g_object_unref (model);

  return view;
}

GtkTreeModel *
create_and_fill_key_model (Windows_Info_Group &win_infos)
{
	//IF_NULL_RETURN_VALUE(win_infos, NULL);
  GtkTreeIter toplevel, child;

  treestore = gtk_tree_store_new(COLS_NUM,
                  G_TYPE_STRING);

  gtk_tree_store_append(treestore, &toplevel, NULL);
  gtk_tree_store_set(treestore, &toplevel,
                     COLUMN, /*"Scripting languages"*/
                     (win_infos.get_specified_windows_information(0))->
                     get_directories(Windows_Information::WINDOWS_ROOT).c_str(),
                     -1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  gtk_tree_store_set(treestore, &child,
                     COLUMN, "Python",
                     -1);
  gtk_tree_store_append(treestore, &child, &toplevel);
  gtk_tree_store_set(treestore, &child,
                     COLUMN, "Perl",
                     -1);
  gtk_tree_store_append(treestore, &child, &toplevel);
  gtk_tree_store_set(treestore, &child,
                     COLUMN, "PHP",
                     -1);

  gtk_tree_store_append(treestore, &toplevel, NULL);
  gtk_tree_store_set(treestore, &toplevel,
                     COLUMN, (win_infos.get_specified_windows_information(0))->get_directories(Windows_Information::WINDOWS_TEMP).c_str(),
                     -1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  gtk_tree_store_set(treestore, &child,
                     COLUMN, "C",
                     -1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  gtk_tree_store_set(treestore, &child,
                     COLUMN, "C++",
                     -1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  gtk_tree_store_set(treestore, &child,
                     COLUMN, "Java",
                     -1);

  return GTK_TREE_MODEL(treestore);
}

GtkWidget *
create_key_view_and_model (GtkWidget *view, Windows_Info_Group &win_infos)
{
  GtkTreeViewColumn *col;
  GtkCellRenderer *renderer;
  GtkTreeModel *model;

  IF_NULL_RETURN_VALUE(view, NULL);

  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "Register key");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer,
      "text", COLUMN);

  model = create_and_fill_key_model(win_infos);
  gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
  g_object_unref(model);

  return view;
}

int
main(int argc, char **argv)
{
    GladeXML *xml = NULL;
    GtkWidget *form = NULL;
	GtkWidget *key_view = NULL;
	GtkWidget *value_view = NULL;

    gtk_init(&argc, &argv);
    xml = glade_xml_new("./ui.glade", NULL, NULL);

	Windows_Info_Group & win_infos = windows_searcher::get_information_of_windows();
	Hive_Accessor reg(win_infos.get_specified_windows_information(0));
	reg.reg_open(HKEY_LOCAL_MACHINE, "SYSTEM\\ControlSet001\\Control");
	//reg.delete_value("jx");
	wchar_t *val = NULL;
	int r = 0;
	reg.get_value("WOW\\SharedWowTimeout", r);
	printf("%d\n", r);
	printf("len = %d \n", reg.get_value("CurrentUser", val));
	//reg.value_modify("jx", 22);
	//reg.remove_allkeys("AppEvents\\Schemes\\Apps");
	reg.reg_close();

    /* get a widget (useful if you want to change something) */
    form = glade_xml_get_widget(xml, "window1");
    //value_view = glade_xml_get_widget(xml, "treeview2");
    key_view = glade_xml_get_widget(xml, "treeview1");
	//create_value_view_and_model (value_view);

	create_key_view_and_model (key_view, win_infos);
    /* connect signal handlers */
    glade_xml_signal_autoconnect(xml);
	gtk_widget_show_all(form);
    gtk_main();

    return 0;
}
